package demo.miner.tools;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;

import jtech.JTech;
import jtech.core.AwtSystem;
import demo.miner.ClassEntity;
import demo.miner.ClassEntity.ClassName;
import demo.miner.ClassUnit;
import demo.miner.EntityUnit;
import demo.miner.system.Configure;

public class SpriteMain extends JTech {
	private static BufferedImage background;

    public static void main(String[] args) {
        System.setProperty("sun.awt.enableExtraMouseButtons", "true");
        try {
	        Rectangle rect = new Rectangle(0, 0, AwtSystem.getInstance().getDesktopWidth(), AwtSystem.getInstance().getDesktopHeigth());
	        background = new Robot().createScreenCapture(rect);
        } catch (Exception e) {
        	throw new RuntimeException(e);
        }
        
        new SpriteMain().startGame();
    }
    
    private int x = 199;
    private int y = 149;
    private Range frame = new Range(0, 0, 0);
    private Range direction = new Range(2, 0, 7);
    
	private int[] animStay   = { 0 };
	private int[] animMove   = { 0, 2, 1, 0, 4, 3, 4 };
	private int[] animAttack = { 5, 6 };
	private int[] anim = animStay;
    private String animName = "Stay";
	
    private Sprite sprite;
    
    @Override
    public void init() {
        setFrameRate(60);
        setWindowMode(400, 300);
        
        setMouseVisible(true);
        setRelativeMouseMode(false);
        
        sprite = new Sprite();
        sprite.setPixelOffsetX(-36);
        sprite.setPixelOffsetY(-36);
        sprite.setPixelWidth(72);
        sprite.setPixelHeight(72);
        sprite.setImagePath("data/png/archery.png");
        sprite.loadImage();
    }

    @Override
    public void shutdown() {
        // TODO Auto-generated method stub
        
    }
    
    public void update(float elapsedTime) {
    	
        if (isKeyPressed(KEY_ESC)) {
            stopGame();
            return;
        }
        
        if (isKeyPressed(KEY_1)) {
        	animName = "Stay";
        	anim = animStay;
        	frame.set(0, 0, anim.length-1);
        } else if (isKeyPressed(KEY_2)) {
        	animName = "Move";
        	anim = animMove;
        	frame.set(0, 0, anim.length-1);
        } else if (isKeyPressed(KEY_3)) {
        	animName = "Attack";
        	anim = animAttack;
        	frame.set(0, 0, anim.length-1);
        }
        
        if (isKeyPressed(KEY_LEFT)) {
        	direction.down();
        } else if (isKeyPressed(KEY_RIGHT)) {
        	direction.up();
        }
        
        if (isKeyPressed(KEY_UP)) {
        	frame.up();
        } else if (isKeyPressed(KEY_DOWN)) {
        	frame.down();
        }
        
    }
    
    @Override
    public void draw(Graphics2D g2d) {
    	String frames = String.format("Frames %s/%s", getFps(), getFrameRate());

//    	g2d.setBackground(Color.WHITE);
//        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//        g2d.clearRect(0, 0, getWindowSize().width, getWindowSize().height);
    	
    	  g2d.drawImage(background,
          		0, 0, getScreenWidth(), getScreenHeight(),
          		getWindow().getLocation().x, getWindow().getLocation().y,
           		getWindow().getLocation().x + getScreenWidth(),
           		getWindow().getLocation().y + getScreenHeight(), null);
    	
        
        g2d.setColor(Color.BLACK);
        g2d.drawString(frames, 10, 10);
        
        getDrawer().black().rect(0, 0, 399, 299);
        getDrawer().line(0, 149, 399, 149);
        getDrawer().line(199, 0, 199, 299);
        
        getDrawer().printc(10, 20, "Anim: %s", animName);
        getDrawer().printc(10, 30, "fram: %s", frame.getValue());
        
        
        sprite.drawImage(getDrawer(), x, y, direction.getValue(), anim[frame.getValue()]);
        sprite.drawDebug(getDrawer(), x, y);
        
        
    }

}
